package content
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	public class Cell extends Sprite
	{
		private var titleTxt:TextField;
		private var _filePic:DisplayObject;
		private var _modelPic:DisplayObject;
		private var _folderInfo:MovieClip;
		private var _titleSprite:Sprite;

		private var _modelVO:ModelVO
		private var _backPic:Shape;

		public function Cell()
		{
			addBacks();
			addPics();
			addTitles();

			addTexts();
			addEvents();

		}

		private function addTitles():void
		{
			_titleSprite=new Sprite;
			_titleSprite.x=20;
			addChild(_titleSprite);

			_modelPic=UiManege.getClass("pic_image_4");
			_modelPic.x=0
			_modelPic.y=2
			_modelPic.visible=false;
			_titleSprite.addChild(_modelPic);


			_filePic=UiManege.getClass("pic_image_wj");
			_filePic.x=0
			_filePic.y=2
			_filePic.visible=false;
			_titleSprite.addChild(_filePic);
		}

		private function addEvents():void
		{
			_folderInfo.addEventListener(MouseEvent.CLICK, onClik)
			_titleSprite.addEventListener(MouseEvent.MOUSE_DOWN, cellMouseDown)
			_titleSprite.addEventListener(MouseEvent.MOUSE_UP, cellMouseUp)
			_titleSprite.addEventListener(MouseEvent.DOUBLE_CLICK,cellDoubleClik)
			_titleSprite.addEventListener(MouseEvent.CLICK,cellClik)
			_titleSprite.doubleClickEnabled =true;
			titleTxt.addEventListener(FocusEvent.FOCUS_OUT,titleTxtFocusOut)
		}
		
		protected function cellClik(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			List(parent.parent).selectCell(_modelVO.id);
		}
		protected function titleTxtFocusOut(event:FocusEvent):void
		{
			_modelVO.name=titleTxt.text.length>2?titleTxt.text:_modelVO.name;
			titleTxt.mouseEnabled=false;
			titleTxt.type = TextFieldType.DYNAMIC;
		}
		
		protected function cellDoubleClik(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			titleTxt.mouseEnabled=true;
			titleTxt.type = TextFieldType.INPUT;
			
	
		}
		
		protected function cellMouseUp(event:MouseEvent):void
		{
			if (_modelVO.filetype == 1)
			{
				UiManege.fileListView.changeModelVoFather(_modelVO.id);
			}

		}

		protected function cellMouseDown(event:MouseEvent):void
		{
			UiManege.fileListView.moveModelvo(_modelVO);
		}

		private function addPics():void
		{
			_folderInfo=UiManege.getClass("mc_folder_info");
			_folderInfo.x=5
			_folderInfo.y=10
			addChild(_folderInfo);



		}

		private function addTexts():void
		{
			var thistextformat:TextFormat=new TextFormat;
			titleTxt= new TextField()
			titleTxt.defaultTextFormat = thistextformat;
			titleTxt.autoSize = TextFieldAutoSize.LEFT;
			titleTxt.htmlText="";
			titleTxt.type = TextFieldType.INPUT;
			titleTxt.type = TextFieldType.DYNAMIC;
			titleTxt.mouseEnabled=false;
			_titleSprite.addChild(titleTxt);
			titleTxt.x=22;
			titleTxt.y=3
				
		

		}

		private function onClik(event:MouseEvent):void
		{
			_modelVO.open=!_modelVO.open;
			List(parent.parent).changePasition();
			
			setSate();
		}

		public function setSate():void
		{
			titleTxt.htmlText=_modelVO.name
			if (_modelVO.open)
			{
				_folderInfo.gotoAndStop(2)
			}
			else
			{
				_folderInfo.gotoAndStop(1)
			}
			if (_modelVO.filetype == 1)
			{
				_filePic.visible=true;
				_folderInfo.visible=true;
			}
			else
			{
				_modelPic.visible=true;
				_folderInfo.visible=false;
			}
			if(_modelVO.select){
				drawBackColor(1);
				
			}else{
				drawBackColor(0);
			}
			if(_modelVO.haveson){
				_folderInfo.visible=true;
			}else{
				_folderInfo.visible=false;
			}
	
		}
		private function drawBackColor(num:int):void
		{
			_backPic.graphics.clear();
			if(num==0){
				_backPic.graphics.clear();
			}
			if(num==1){
				_backPic.graphics.beginFill(0x000000, 0.1);
				_backPic.graphics.drawRect(40, 2,5+ titleTxt.width, 20);
				_backPic.graphics.endFill();
			}
		}

		public function set source(tempVo:ModelVO):void
		{
			_modelVO=tempVo
			if (tempVo)
			{
				setSate();
			}
		}
		private function addBacks():void
		{
			_backPic=new Shape;
			addChild(_backPic);
	
		}
	}
}
